/*
 * 项目名称:项目名称
 * 类名称:MldCustomReservationServiceImpl.java
 * 包名称:com.tongyi.modules.mld.service.impl
 * @author 美琳达
 * @date 2024-06-26 21:29:56
 * Copyright (c) 2019-2021 惠州市酷天科技有限公司
 */
package com.tongyi.modules.mld.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.CaseFormat;
import com.tongyi.core.PageInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tongyi.common.utils.Query;
import com.tongyi.modules.mld.dao.MldCustomReservationDao;
import com.tongyi.modules.mld.entity.MldCustomReservationEntity;
import com.tongyi.modules.mld.service.MldCustomReservationService;
import com.tongyi.modules.sys.entity.SysUserEntity;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.Serializable;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * Service实现类
 *
 * @author 美琳达
 * @date 2024-06-26 21:29:56
 */
@Service("mldCustomReservationService")
public class MldCustomReservationServiceImpl extends ServiceImpl<MldCustomReservationDao, MldCustomReservationEntity> implements MldCustomReservationService {

    @Override
    public MldCustomReservationEntity getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public List<MldCustomReservationEntity> listAll(Map<String, Object> params) {
        return super.baseMapper.listAll(params);
    }

    @Override
    public PageInfo<MldCustomReservationEntity> listPage(int current, int size, Map<String, Object> params) {
        Page<MldCustomReservationEntity> page = new Query<MldCustomReservationEntity>(current, size, params).getPage();
        List<MldCustomReservationEntity> list = super.baseMapper.listPage(page, params);
        return new PageInfo<MldCustomReservationEntity>(page.getCurrent(), page.getSize(), page.getTotal()).setList(list);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addEntity(MldCustomReservationEntity entity) {
        return super.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateEntity(MldCustomReservationEntity entity) {
        return super.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteEntity(Serializable id) {
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(Serializable[] ids) {
        return super.removeByIds(Arrays.asList(ids));
    }

    @Override
    public PageInfo<MldCustomReservationEntity> listPage(int current, int size, Map<String, Object> params, SysUserEntity sysUserEntity) {
        params.remove("page");
        params.remove("limit");
        Page<MldCustomReservationEntity> page = new Page<>(current, size);
        QueryWrapper<MldCustomReservationEntity> wrapper = new QueryWrapper<>();
        if (MapUtils.isNotEmpty(params)) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                wrapper.eq(Objects.nonNull(entry.getValue()) && !"".equals(entry.getValue()), CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, entry.getKey()), entry.getValue());
            }
        }
        wrapper.and(m -> {
            m.isNull("user_id").or().eq("user_id", sysUserEntity.getUserId()).or().eq("user_id", "");
        }).orderByAsc("id");
        List<MldCustomReservationEntity> mldCustomReservationEntities = super.baseMapper.selectPage(page, wrapper).getRecords();
        page.setRecords(mldCustomReservationEntities);
        return new PageInfo<MldCustomReservationEntity>(page.getCurrent(), page.getSize(), page.getTotal()).setList(page.getRecords());
    }
}
